package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class AdvimgController {


    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private AdvposService advposService;




    //分页查询
    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, Long page, Long limit){

        //创建分页构造器
        Page<Advimg> pageinfo = new Page(page,limit);

        //创建条件查询构造器
        LambdaQueryWrapper<Advimg> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(StringUtils.isNotEmpty(advimgdesc),Advimg::getAdvimgdesc,advimgdesc);
        lqw.like(StringUtils.isNotEmpty(advimgpos),Advimg::getAdvimgpos,advimgpos);


        Page<Advimg> advimgPage = advimgService.page(pageinfo, lqw);
        //继承查询到的数据总数
        long count = (long) advimgService.count(lqw);
        //将数据取出
        List<Advimg> data = advimgPage.getRecords();

        //遍历数据 进行对拷
        List<AdvimgDto> list = data.stream().map((item) -> {
            //创建Dto对象
            AdvimgDto advimgDto = new AdvimgDto();
            //对象拷贝
            BeanUtils.copyProperties(item,advimgDto);

            //取出tAdvimgpos属性值
            String advimgpos1 = item.getAdvimgpos();


            LambdaQueryWrapper<Advpos> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            //两个表的关联属性是否一致
            lambdaQueryWrapper.eq(advimgpos1!=null,Advpos::getId,advimgpos1);
            //调用方法查询到符合条件的对象
            Advpos advpos = advposService.getOne(lambdaQueryWrapper);
            //得到advpos表中的name数据
            String advposAdvposname = advpos.getAdvposname();
            //拷贝给封装的dto
            advimgDto.setAdvposname(advposAdvposname);

            return advimgDto;
            }).collect(Collectors.toList());


        return ResultData.ok("",PageResult.init(count,limit,page, list));

    }


    //添加
    @PostMapping("/add")
    public  ResultData save(@RequestBody Advimg advimg){

        advimgService.save(advimg);

        return ResultData.ok("添加成功", null);

    }


    //回显
    @GetMapping("/rowInfo")
    public ResultData getById( int id){

        Advimg advimg = advimgService.getById(id);

        if (advimg != null) {
            return ResultData.ok("",advimg);
        }
        return ResultData.error("没有查到相关信息");
    }


    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody Advimg advimg){

        advimgService.updateById(advimg);

        return ResultData.ok("修改成功",null);

    }


    //删除
    @GetMapping("del")
    public ResultData delete(int id){

        advimgService.removeById(id);

        return ResultData.ok("删除成功",null);

    }







}
